package com.ouheng.shop.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ouheng.shop.dao.PremiumDao;
import com.ouheng.shop.domain.PremiumDO;
import com.ouheng.shop.domain.ProductPriceDO;
import com.ouheng.shop.service.PremiumService;



@Service
public class PremiumServiceImpl implements PremiumService {
	@Autowired
	private PremiumDao premiumDao;
	
	@Override
	public PremiumDO get(Long id){
		return premiumDao.get(id);
	}
	
	@Override
	public List<PremiumDO> list(Map<String, Object> map){
		return premiumDao.list(map);
	}
	
	@Override
	public int count(Map<String, Object> map){
		return premiumDao.count(map);
	}
	
	@Override
	@Transactional
	public int save(PremiumDO premium){
		List<PremiumDO> listPremium = premiumDao.list(null);
		if(listPremium.isEmpty()){
			premium.setStatus("2");
		}else{
			if("2".equals(premium.getStatus())){
				Map<String, Object> map = new HashMap<>();
				map.put("status", 2);
				List<PremiumDO> listPremiumDO = premiumDao.list(map);
				for (PremiumDO PremiumDO : listPremiumDO) {
					PremiumDO.setStatus("1");
					premiumDao.update(PremiumDO);
				}
			}
		}
		return premiumDao.save(premium);
	}
	
	@Override
	@Transactional
	public int update(PremiumDO premium){
		if("2".equals(premium.getStatus())){
			Map<String, Object> map = new HashMap<>();
			map.put("status", 2);
			List<PremiumDO> list = premiumDao.list(map);
			if(!list.isEmpty()){
				for (PremiumDO premiumDO : list) {
					premiumDO.setStatus("1");
					premiumDao.update(premiumDO);
				}
			}
		}
		return premiumDao.update(premium);
	}
	
	@Override
	@Transactional
	public int remove(Long id){
		return premiumDao.remove(id);
	}
	
	@Override
	@Transactional
	public int batchRemove(Long[] ids){
		return premiumDao.batchRemove(ids);
	}
	
}
